package com.xxx.video.mine.fragments;

import android.os.Bundle;


import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.dbs.beans.SearchHistoryBean;
import com.xxx.video.commen.dbs.utils.DbUtils;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.main.activitys.SearchActivity;
import com.xxx.video.main.activitys.VideoDetailsActivity;
import com.xxx.video.main.beans.IndexBean;
import com.xxx.video.mine.adapters.SearchRecommendListAdapter;
import com.xxx.video.mine.beans.SearchHotWordListBean;
import com.xxx.video.mine.presenters.ISearchRecommendView;
import com.xxx.video.mine.presenters.SearchRecommendPresenter;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class SearchRecommendFragment extends BaseFragment<SearchRecommendPresenter>
        implements ISearchRecommendView, BaseRecycleViewAdapter.OnItemClickListener {

    @BindView(R.id.rcv_search_recommend_fragment_list)
    RecyclerView rcvSearchRecommendFragmentList;
    /**
     * adapter
     */
    private SearchRecommendListAdapter mSearchRecommendListAdapter;


    public static SearchRecommendFragment newInstance(Bundle args) {
        SearchRecommendFragment fragment = new SearchRecommendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected SearchRecommendPresenter createPresenter() {
        return new SearchRecommendPresenter(Objects.requireNonNull(getActivity()), this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_search_recommend;
    }

    @Override
    public void initData(Bundle extras) {
        initList();
        getPresenter().getHistoryKeyword();
        getPresenter().getSearchHistoryList();
    }

    @Override
    public void refreshData(Bundle extras) {
        getPresenter().getHistoryKeyword();
        getPresenter().getSearchHistoryList();
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }


    @Override
    public void initData2View(IndexBean successOb) {
        showRootLayoutStatus(1);
    }

    @Override
    public void startVideoDetailsActivity(String videoId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", videoId);
        intent2Activity(VideoDetailsActivity.class, bundle);
    }

    @Override
    public void initList() {
        rcvSearchRecommendFragmentList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mSearchRecommendListAdapter = new SearchRecommendListAdapter(Objects.requireNonNull(getActivity()));
        mSearchRecommendListAdapter.setOnItemClickListener(this);
        rcvSearchRecommendFragmentList.setAdapter(mSearchRecommendListAdapter);
    }

    @Override
    public void initSearchHistoryListData2View(List<SearchHistoryBean> searchHistoryBeanList) {
        if (mSearchRecommendListAdapter != null) {
            mSearchRecommendListAdapter.setSearchHistoryListData(searchHistoryBeanList);
        }
    }

    @Override
    public void initSearchHotWordListBeanData2View(SearchHotWordListBean successOb) {
        if (mSearchRecommendListAdapter != null) {
            mSearchRecommendListAdapter.setSearchHotWordListData(successOb.list);
        }
    }

    @Override
    public void onItemClick(Object object, int position, int type) {
        if (type == 2) {
            try {
                SearchHotWordListBean.ListBean listBean = (SearchHotWordListBean.ListBean) object;
                ((SearchActivity) getActivity()).search(listBean.fKeyword);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (type == 3) {
            try {
                SearchHistoryBean searchHistoryBean = (SearchHistoryBean) object;
                ((SearchActivity) getActivity()).search(searchHistoryBean.searchWord);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (type == 4) {
            DbUtils.getInstance().clearAllSearchHistoryListBean();
            getPresenter().getSearchHistoryList();
        }
    }
}
